﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows;
using UIWindows.Models.ParameterModels;

namespace UIWindows.GlobalStatics
{
    [Attributes.Singleton]
    public  class MesParameterStatic
    {
        public ObservableCollection<MesOutputParameterModel> MesOutputParameters { get; set; } = new ObservableCollection<MesOutputParameterModel>();
        public ObservableCollection<MesRunParameterModel> MesRunParameters { get; set; } = new ObservableCollection<MesRunParameterModel>();
        public MesParameterStatic()
        {
            if (File.Exists(GlobalStatic.MesParameterPath))
            {
                try
                {
                    var model = JsonSerializer.Deserialize<Dictionary<string, object>>(File.ReadAllText(GlobalStatic.MesParameterPath, System.Text.Encoding.UTF8));
                    MesRunParameters = JsonSerializer.Deserialize<ObservableCollection<MesRunParameterModel>>(model["MesRunParameters"].ToString(), GlobalStatic.Options);
                    MesOutputParameters = JsonSerializer.Deserialize<ObservableCollection<MesOutputParameterModel>>(model["MesOutputParameters"].ToString(), GlobalStatic.Options);
                }
                catch (Exception ex)
                {

                    MessageBox.Show($"注意==>>MES参数初始化异常：{ex}");
                }
            }
           
        }
        public void Save() => System.IO.File.WriteAllText(GlobalStatic.MesParameterPath, JsonSerializer.Serialize(this, GlobalStatic.Options), Encoding.UTF8);
    }
}
